﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

namespace SouthernLand.Common
{
    public enum EnumUpload
    {
        FileNull,
        FileExits,
        FileLarge,
        FileExtensionNotAgree,
        UploadSucceed
    }

    public class FileProcess
    {
        public FileProcess()
        {
        }

        public static Size CalculateDimensions(Size oldSize, int targetSize)
        {
            Size size = new Size();
            if (oldSize.Height > oldSize.Width)
            {
                size.Width = (int)(oldSize.Width * (((float)targetSize) / ((float)oldSize.Height)));
                size.Height = targetSize;
                return size;
            }
            size.Width = targetSize;
            size.Height = (int)(oldSize.Height * (((float)targetSize) / ((float)oldSize.Width)));
            return size;

        }

        public static int DeleteFile(string strFilePath)
        {
            if (File.Exists(strFilePath))
            {
                File.Delete(strFilePath);
                return 1;
            }
            return -1;

        }

        public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
        {
            byte[] buffer;
            using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
            {
                Size size = CalculateDimensions(image.Size, targetSize);
                using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.SmoothingMode = SmoothingMode.AntiAlias;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        graphics.DrawImage(image, new Rectangle(new Point(0, 0), size));
                        MemoryStream stream = new MemoryStream();
                        bitmap.Save(stream, ImageFormat.Jpeg);
                        buffer = stream.GetBuffer();
                    }
                }
            }
            return buffer;

        }

        public static EnumUpload UpLoadFile(FileUpload strObject, string strFilePath)
        {
            if (strObject.PostedFile != null)
            {
                HttpPostedFile postedFile = strObject.PostedFile;
                int contentLength = postedFile.ContentLength;
                byte[] buffer = new byte[contentLength];
                postedFile.InputStream.Read(buffer, 0, contentLength);
                WriteToFile(buffer, strFilePath);
                return EnumUpload.UploadSucceed;
            }
            return EnumUpload.FileNull;

        }

        public static EnumUpload UpLoadImage(FileUpload strObject, string strFilePath, int photoSize)
        {
            if (strObject.PostedFile != null)
            {
                HttpPostedFile postedFile = strObject.PostedFile;
                int contentLength = postedFile.ContentLength;
                byte[] buffer = new byte[contentLength];
                postedFile.InputStream.Read(buffer, 0, contentLength);
                if (photoSize > 0)
                {
                    System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(buffer));
                    if ((image.Size.Width > photoSize) || (image.Size.Height > photoSize))
                    {
                        buffer = ResizeImageFile(buffer, photoSize);
                    }
                }
                WriteToFile(buffer, strFilePath);
                return EnumUpload.UploadSucceed;
            }
            return EnumUpload.FileNull;

        }

        public static void WriteToFile(byte[] dataFile, string strFilePath)
        {
            FileStream stream = new FileStream(strFilePath, FileMode.Create, FileAccess.ReadWrite);
            stream.Write(dataFile, 0, dataFile.Length);
            stream.Flush();
            stream.Close();

        }
    }
}
